package breeze.pixel.weather.typhoon;

import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

import breeze.pixel.weather.BaseView;
import breeze.pixel.weather.R;
import breeze.pixel.weather.apps_util.views.mtextview.MTextView;

public class TyphoonInfoActivity extends BaseView {

    public String TAG = TyphoonInfoActivity.class.getSimpleName();

    private MTextView typhoon_name;
    private MTextView typhoon_start;
    private MTextView typhoon_end;
    private MTextView typhoon_life;
    private MTextView typhoon_status;
    private LinearLayout typhoon_things;
    private final List<TyphoonImportantThings> importantThings=new ArrayList<TyphoonImportantThings>();//台风重要事件

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        setContentView(R.layout.activity_typhoon);
        //获取控件
        init();
        //获取数据
        getData();
    }

    @Override
    public void init() {
        typhoon_name=findViewById(R.id.typhoon_name);
        typhoon_start=findViewById(R.id.typhoon_start);
        typhoon_end=findViewById(R.id.typhoon_end);
        typhoon_life=findViewById(R.id.typhoon_life);
        typhoon_things=findViewById(R.id.typhoon_things);
        typhoon_status=findViewById(R.id.typhoon_status);
    }

    @Override
    public void initData() {

    }

    private void getData() {
        BaseView.getExec().execute(new Runnable(){
                public void run() {
                    try {
                        importantThings.removeAll(importantThings);
                        String data="http://m.weathercn.com/typhoon.do?partner=shenma17&id=101924&p_source=card&p_type=jump";
                        Document doc=Jsoup.connect(data).get();
                        Elements ele1=doc.getElementsByClass("typhoon");
                        Elements ele2=ele1.select("h2").get(0).getElementsByTag("h2");
                        String typhoonName=ele2.text();//台风名称
                        Elements ele3=ele1.select("section[class='lifetime']");
                        Elements ele4=ele3.select("p");
                        String typhoonLifeStart=ele4.get(0).text();//台风开始时间
                        String typhoonLifeStop=ele4.get(1).text();//台风结束时间
                        String typhoonLifeTime=ele4.get(2).text();//台风存活时间
                        Elements ele5=ele1.select("section[class='course']");
                        Elements ele6=ele5.select("li");
                        for (Element e:ele6) {
                            Elements ele7=e.select("span");
                            String thing=ele7.text();
                            String TimeAndThing=e.text();
                            String time=TimeAndThing.replace(thing,"");
                            TyphoonImportantThings tt= new TyphoonImportantThings();
                            tt.setTime(time);
                            tt.setThing(thing);
                            importantThings.add(tt);
                        }
                        setText(typhoon_name,typhoonName);
                        setText(typhoon_start,typhoonLifeStart);
                        setText(typhoon_end,typhoonLifeStop);
                        setText(typhoon_life,typhoonLifeTime);
                        runOnUiThread(new Runnable(){
                                public void run() {
                                    for(TyphoonImportantThings tt:importantThings){
                                        View view=LayoutInflater.from(TyphoonInfoActivity.this).inflate(R.layout.typhoon_item,null);
                                        MTextView w1=view.findViewById(R.id.typhoon_thing_time);
                                        w1.setText(tt.getTime());
                                        MTextView w2=view.findViewById(R.id.typhoon_thing);
                                        w2.setText(tt.getThing());
                                        typhoon_things.addView(view);
                                    }
                                }
                        });
                    } catch (Exception e) {
                        setText(typhoon_status,"获取失败！");
                    }
                }
            });
    }

    private void setText(final MTextView view, final String data) {
        runOnUiThread(new Runnable(){
                public void run() {
                    view.setText(data);
                }
            });
    }

    static class TyphoonImportantThings {
        String time,things;
        public void setTime(String s) {
            this.time=s;
        }
        public String getTime() {
            return time;
        }
        public void setThing(String s) {
            this.things=s;
        }
        public String getThing() {
            return things;
        }
    }

}
